package com.shiwaixiangcun.customer.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Author   Administrator
 * Date     2018/4/8
 * Desc     eg
 */
public class SignUtils {

    public static String sign(Map<String, String> map) {
        String params = sortMapKey(map);

        return MD5Util.md5(params);
    }


    public static boolean verification(Map<String, String> map, String test) {
        String sign = sign(map);
        return sign.equals(test);
    }

    public static String sortMapKey(Map<String, String> map) {
        List<Map.Entry<String, String>> infoIds = new ArrayList<>(map.entrySet());
//        infoIds.sort(Comparator.comparing(o -> (o.getKey())));
        // 构造URL 键值对的格式
        StringBuilder buf = new StringBuilder();
        for (Map.Entry<String, String> item : infoIds) {
            if (!item.getKey().isEmpty()) {
                String key = item.getKey();
                String val = item.getValue();
                buf.append(key).append("=").append(val);
                buf.append("&");
            }
        }
        String buff = buf.toString();
        if (!buff.isEmpty()) {
            buff = buff.substring(0, buff.length() - 1);
        }
        return buff;
    }

}
